package jackaroo.web.converter;

import jackaroo.pojo.Role;
import org.springframework.core.convert.converter.Converter;
import org.springframework.util.StringUtils;

import java.io.UnsupportedEncodingException;

/**
 * @author JackarooZhang
 * @date 2018/6/18 11:12
 */
public class StringToRoleConverter implements Converter<String, Role> {
    @Override
    public Role convert(String source) {
        try {
            source = new String(source.getBytes("ISO8859-1"), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        if (StringUtils.isEmpty(source)) {
            return null;
        }
        if (source.indexOf("-") == -1) {
            return null;
        }
        String[] arr = source.split("-");
        if (arr.length != 3) {
            return null;
        }
        Role role = new Role();
        role.setId(Long.parseLong(arr[0]));
        role.setRoleName(arr[1]);
        role.setNote(arr[2]);
        return role;
    }
}